﻿using Loogn.DBTest.BLL;
using Loogn.DBTest.DAL;
using Loogn.DBTest.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Loogn.DBTest
{
    class Program
    {
        /// <summary>
        /// 只测试数据访问，所以直接用DAL
        /// </summary>
        static UserDAL dal = new UserDAL();
        static int flag = 0;
        static void Main(string[] args)
        {
          //  Console.WriteLine(typeof(dynamic));
            //Add1();
            //Add2();
            //DelAll();
            //Update1();
            //Update2();
            //SelectOne(1);
            //SelectList();
            //SelectPage(1, 1);
            //SelectPage(2, 1);
            
        }

        #region 简单的增删改查
        /// <summary>
        /// 插入完整信息
        /// </summary>
        static void Add1()
        {
            var addInfo = new UserInfo
            {
                //ID = 0,//[DBField(InsertIgnore=true)] 自增忽略
                Age = 25,
                Date = DateTime.Now,
                Name = "loogn",
                Sex = true,
                Remark = "remoakse"
            };
            flag = dal.Add(addInfo);
            Console.WriteLine(flag);   
        }

        /// <summary>
        /// 如果数据库有默认值，可指定部分字段插入
        /// </summary>
        static void Add2()
        {
            var partModel = new Dictionary<string, object> { 
                {"Name","王胜龙"},
                {"Age",26}
            };
            flag = dal.Add(partModel);
            Console.WriteLine(flag);
        }

        /// <summary>
        /// 删除表中所有数据
        /// </summary>
        static void DelAll()
        {
            flag= dal.Delete();
            Console.WriteLine(flag);
        }

        /// <summary>
        /// 删除指定条件的数据
        /// </summary>
        static void Del()
        {
            flag= dal.Delete("ID=3 and Name='loogn'");
            Console.WriteLine(flag);
        }

        /// <summary>
        /// 根据主键修改行信息，有[DBField(UpdateIgnore=true)]属性的字段除外,所以Remark没有修改
        /// </summary>
        static void Update1()
        {
            var addInfo = new UserInfo
            {
                ID = 7,//[DBField(IsPrimaryKey=true)]
                Age = 1,
                Date = DateTime.Now,
                Name = "loogn update",
                Sex = false
            };
            flag = dal.Update(addInfo);
            Console.WriteLine(flag);
        }

        /// <summary>
        /// 根据条件修改指定字段
        /// </summary>
        static void Update2()
        {
            var updateFields = new Dictionary<string, object> { 
                {"Name","王胜龙"},
                {"$Age","Age+23"},//key以$开头，表直接为Age=Age+23，而不参数化
                {"Remark","这次修改remark"}
            };
            flag = dal.Update(updateFields, "ID=8");
            Console.WriteLine(flag);
        }

        /// <summary>
        /// 根据条件查询一个
        /// </summary>
        /// <param name="id"></param>
        static void SelectOne(int id)
        {
            var info = dal.FindOne("ID=" + id, "*");
            Print(info);
        }

        /// <summary>
        /// 根据条件和排序查询前两个
        /// </summary>
        static void SelectList()
        {
            var list = dal.FindList("Age>2", null, 2, "id desc");
            Print(list);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        static void SelectPage(int pageIndex, int pageSize)
        {
            var pr = dal.FindPage(null, null, "id desc", pageIndex, pageSize, true);
            Console.WriteLine("pageIndex:{0},pageSize:{1},Total:{2}", pr.PageIndex, pr.PageSize, pr.TotalCount);
            Print(pr.DataList);
        }

        #endregion


        #region 原生操作

        //写原生sql
        static void opt1()
        {
            //flag = dal.(System.Data.CommandType.Text, "delete from [User] where ID=1");
            Console.WriteLine(flag);

            //var list= dal.ExecuteList(System.Data.CommandType.Text, "select * from [User]");
            //Print(list);
            //等等......
            //等等......
            //等等......
        }

        //事务
        static void opt2()
        {
            dal.TransTest();
        }

        #endregion


        static void Print(UserInfo info)
        {
            Console.WriteLine("ID:{0} Name:{1} Sex:{2} Age:{3}", info.ID, info.Name, info.Sex, info.Age);
            Console.WriteLine("{0}--{1}", info.Remark, info.Date);
            Console.WriteLine("*************************************************");
        }
        static void Print(List<UserInfo> list)
        {
            foreach (var info in list)
            {
                Print(info);
            }
        }
    }
}
